//
//  GJFInspirationCollectionViewController.m
//  Wedding
//
//  Created by 郭静芬 on 2017/1/8.
//  Copyright © 2017年 郭静芬. All rights reserved.
//

#import "GJFInspirationDetailViewController.h"
#import "GJFInspirationCollectionViewCell.h"
#import "GJFToolView.h"
#import "GJFSuccessViewController.h"
#import "GJFCaseDetailViewController.h"

@interface GJFInspirationDetailViewController () <UICollectionViewDelegate, UICollectionViewDataSource, GJFToolViewDelegate, UMSocialShareMenuViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) GJFToolView *toolView;
@property (nonatomic, strong) GJFInspirationModel *currentModel;
@end

@implementation GJFInspirationDetailViewController

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = HOMECOLOR;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20], NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self.navigationController.navigationBar setBackgroundImage:[GJFSingleTool imageWithColor:HOMECOLOR] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.tintColor = HOMECOLOR;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20], NSForegroundColorAttributeName:HOMECOLOR}];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar setBackgroundImage:[GJFSingleTool imageWithColor:[UIColor whiteColor]] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon-返回"] style:UIBarButtonItemStyleDone target:self action:@selector(popVC)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"分享"] style:UIBarButtonItemStyleDone target:self action:@selector(clickShareAction)];
    
    [self.collectionView scrollToItemAtIndexPath:self.indexP atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    self.navigationItem.title = [self.inspirationArr[self.indexP.row] casustitle];
    self.currentModel = self.inspirationArr[self.indexP.row];
    self.toolView.plotModel = (GJFPlotModel *)self.currentModel;
}

- (void)popVC {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)clickShareAction {
    [GJFSingleTool shareToViewController:self title:self.currentModel.casustitle content:@"幻紫琉晶" image:nil url:@"http://www.baidu.com/"];
}

#pragma mark - CollectionView分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
#pragma mark - CollectionView每个分区中item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.inspirationArr.count;
}
#pragma mark - CollectionView每个分区中item的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GJFInspirationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"InspirationCell" forIndexPath:indexPath];
    cell.inspirationModel = self.inspirationArr[indexPath.row];
    cell.imageV.contentMode = UIViewContentModeScaleAspectFit;
    return cell;
}
#pragma mark - CollectionView点击跳转
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.navigationController.viewControllers.count == 3) {
        return;
    }
    GJFCaseDetailViewController *caseVC = [[GJFCaseDetailViewController alloc] init];
    caseVC.caseDetailModel = self.inspirationArr[indexPath.row];
    [self.navigationController pushViewController:caseVC animated:YES];
}
//拖拽结束，显示标题，刷新底部视图是否点赞收藏
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = (NSInteger)scrollView.contentOffset.x / WIDTH;
    self.navigationItem.title = [self.inspirationArr[index] casustitle];
    self.currentModel = self.inspirationArr[index];
    self.toolView.plotModel = self.inspirationArr[index];
}

#pragma mark - GJFToolViewDelegate
- (void)clickZanBtnAction {
    NSString *url = [self.currentModel.is_zan boolValue]?APP_PLOT_UNZAN:APP_PLOT_ZAN;
    NSDictionary *dict = @{@"zanId":self.currentModel.myId, @"type":@1};
    [GJFNetworkTool POSTRequestWithUrl:url params:dict complete:^(NSDictionary *result) {
        if ([[result objectForKey:@"code"] intValue] == 1000) {
            self.currentModel.is_zan = [NSString stringWithFormat:@"%d", ![self.currentModel.is_zan boolValue]];
            self.toolView.plotModel = (GJFPlotModel *)self.currentModel;
        }
        [self.view showWarning:[result objectForKey:@"msg"]];
    }];
}
- (void)clickCollectBtnAction {
    NSString *url = [[self.currentModel is_collect] boolValue]?APP_PLOT_UNCOLLECT:APP_PLOT_COLLECT;
    NSDictionary *dict = @{@"collectId":self.currentModel.myId, @"type":@3};
    [GJFNetworkTool POSTRequestWithUrl:url params:dict complete:^(NSDictionary *result) {
        if ([[result objectForKey:@"code"] intValue] == 1000) {
            [self.inspirationArr[_indexP.row] setIs_collect:[NSString stringWithFormat:@"%d", ![[self.inspirationArr[_indexP.row] is_collect] boolValue]]];
            self.toolView.plotModel = self.inspirationArr[_indexP.row];
        }
        [self.view showWarning:[result objectForKey:@"msg"]];
    }];
}
- (void)clickOrderBtnAction {
    //    [self.view showWarning:@"预约商家"];
    GJFSuccessViewController *success = [[GJFSuccessViewController alloc] initWithNibName:NSStringFromClass([GJFSuccessViewController class]) bundle:nil];
    success.merchantTitleStr = [self.inspirationArr[_indexP.row] casustitle];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:success];
    success.navigationItem.title = @"预约申请成功";
    [self presentViewController:navi animated:YES completion:^{}];
}
- (void)clickTelBtnAction {
    NSMutableString * str = [[NSMutableString alloc] initWithFormat:@"telprompt://%@", self.currentModel.telephone];
    NSURL *url = [NSURL URLWithString:str];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }
    else {
        [self.view showWarning:@"不支持电话功能"];
    }
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(WIDTH, HEIGHT - 64 - 49);
        layout.minimumLineSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        [_collectionView registerClass:[GJFInspirationCollectionViewCell class] forCellWithReuseIdentifier:@"InspirationCell"];
        
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

- (GJFToolView *)toolView {
    if (!_toolView) {
        _toolView = [[GJFToolView alloc] initWithFrame:CGRectMake(0, HEIGHT - 64 - 49, WIDTH, 49)];
        _toolView.delegate = self;
        [self.view addSubview:_toolView];
    }
    return _toolView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
